package com.ihr360.cnb.module.insurance.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ihr360.cnb.module.insurance.service.MemberPlanAggregateService;
import com.ihr360.cnb.module.insurance.service.StaffIdCheckService;
import com.ihr360.cnb.module.insurance.service.feign.InsuranceMemberPlanService;
import com.ihr360.commons.vo.PageData;
import com.ihr360.insurance.entity.request.MemberPlanReq;
import com.ihr360.insurance.entity.response.LedgerResponse;
import com.ihr360.insurance.entity.vo.MemberPlanVo;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author Stone.Shi
 * @description 社保公积金增减员
 * @date 2018-05-24 18:00:32.
 */
@RestController
@RequestMapping("/")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.BENEFIT_CODE,
        mainModuleName = ModuleCodeStore.Module.BENEFIT_NAME,
        subModuleCode = ModuleCodeStore.Module.Benefit.LEDGER_STAFF_IN_OUT_CODE,
        subModuleName = ModuleCodeStore.Module.Benefit.LEDGER_STAFF_IN_OUT_NAME)
@DisableResponseAdvice(disabled = false)
public class MemberPlanAggregateController {

    @Autowired
    private StaffIdCheckService staffIdCheckService;

    @Autowired
    private InsuranceMemberPlanService insuranceMemberPlanService;

    @Autowired
    private MemberPlanAggregateService memberPlanAggregateService;

    @FunctionDefinition(functionName = "获取增员列表", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/increaseMemberPlans/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public PageData<MemberPlanVo> getIncreaseMemberList(@RequestParam String companyId,
                                                        @RequestParam String userId,
                                                        @RequestBody MemberPlanReq req) {
        return memberPlanAggregateService.getIncreaseMember(companyId, userId, req);
    }

    @FunctionDefinition(functionName = "增员人员加入台账", functionCode = ModuleCodeStore.Function.ADD)
    @RequestMapping(value = "/increaseMemberPlans/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public LedgerResponse addIncreaseMember(@RequestParam String companyId,
                                            @RequestParam String userId,
                                            @RequestParam Integer year,
                                            @RequestParam Integer month,
                                            @RequestBody MemberPlanVo vo) {
        String staffId = vo.getStaffId();
        ValidationUtils.rejectIfEmpty(staffId, null, "员工数据不存在");
        staffIdCheckService.check(Lists.newArrayList(staffId));
        return insuranceMemberPlanService.addIncreaseMember(companyId, userId, year, month, vo);
    }

    @FunctionDefinition(functionName = "增员人员从台账中移除", functionCode = ModuleCodeStore.Function.DELETE)
    @RequestMapping(value = "/increaseMemberPlans/remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public boolean removeIncreaseMember(@RequestParam String companyId,
                                        @RequestParam String userId,
                                        @RequestParam Integer year,
                                        @RequestParam Integer month,
                                        @RequestBody MemberPlanVo vo) {
        String staffId = vo.getStaffId();
        ValidationUtils.rejectIfEmpty(staffId, null, "员工数据不存在");
        staffIdCheckService.check(Lists.newArrayList(staffId));
        insuranceMemberPlanService.removeIncreaseMember(companyId, userId, year, month, vo);
        return true;
    }


    @FunctionDefinition(functionName = "导出增员数据", functionCode = ModuleCodeStore.Function.EXPORT)
    @RequestMapping(value = "/increaseMemberPlans/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void exportExcelIncreaseMember(@RequestParam String companyId,
                                          @RequestParam String userId,
                                          @RequestParam String reqString,
                                          HttpServletResponse response) throws IOException {
        MemberPlanReq req = new MemberPlanReq();
        try {
            req = JSONObject.parseObject(reqString, MemberPlanReq.class);
        } catch (Exception e) {
            ValidationUtils.reject("", "下载参数错误");
        }
        StringBuilder fileNameBuilder = new StringBuilder();
        fileNameBuilder.append(req.getYear()).append("年").append(req.getMonth()).append("月").append("增员表.xls");
        response.setHeader("x-filename", URLEncoder.encode(fileNameBuilder.toString(), "UTF-8"));
        memberPlanAggregateService.exportExcelIncreaseMember(companyId, userId, req, response.getOutputStream());
    }

    @FunctionDefinition(functionName = "获取减员列表", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/decreaseMemberPlans/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public PageData<MemberPlanVo> getDecreaseMemberList(@RequestParam String companyId,
                                                        @RequestParam String userId,
                                                        @RequestBody MemberPlanReq req) {
        return memberPlanAggregateService.getDecreaseMember(companyId, userId, req);
    }

    @FunctionDefinition(functionName = "减员人员加入台账", functionCode = ModuleCodeStore.Function.ADD)
    @RequestMapping(value = "/decreaseMemberPlans/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public LedgerResponse addDecreaseMember(@RequestParam String companyId,
                                            @RequestParam String userId,
                                            @RequestParam Integer year,
                                            @RequestParam Integer month,
                                            @RequestBody MemberPlanVo vo) {
        String staffId = vo.getStaffId();
        ValidationUtils.rejectIfEmpty(staffId, null, "员工数据不存在");
        staffIdCheckService.check(Lists.newArrayList(staffId));
        return insuranceMemberPlanService.addDecreaseMember(companyId, userId, year, month, vo);
    }

    @FunctionDefinition(functionName = "减员人员从台账中移除", functionCode = ModuleCodeStore.Function.DELETE)
    @RequestMapping(value = "/decreaseMemberPlans/remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public boolean removeDecreaseMember(@RequestParam String companyId,
                                        @RequestParam String userId,
                                        @RequestParam Integer year,
                                        @RequestParam Integer month,
                                        @RequestBody MemberPlanVo vo) {
        String staffId = vo.getStaffId();
        ValidationUtils.rejectIfEmpty(staffId, null, "员工数据不存在");
        staffIdCheckService.check(Lists.newArrayList(staffId));
        insuranceMemberPlanService.removeDecreaseMember(companyId, userId, year, month, vo);
        return true;
    }

    @FunctionDefinition(functionName = "导出减员数据", functionCode = ModuleCodeStore.Function.EXPORT)
    @RequestMapping(value = "/decreaseMemberPlans/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void exportExcelDecreaseMember(@RequestParam String companyId,
                                          @RequestParam String userId,
                                          @RequestParam String reqString,
                                          HttpServletResponse response) throws IOException {
        MemberPlanReq req = new MemberPlanReq();
        try {
            req = JSONObject.parseObject(reqString, MemberPlanReq.class);
        } catch (Exception e) {
            ValidationUtils.reject("", "下载参数错误");
        }
        StringBuilder fileNameBuilder = new StringBuilder();
        fileNameBuilder.append(req.getYear()).append("年").append(req.getMonth()).append("月").append("减员表.xls");
        response.setHeader("x-filename", URLEncoder.encode(fileNameBuilder.toString(), "UTF-8"));
        memberPlanAggregateService.exportExcelDecreaseMember(companyId, userId, req, response.getOutputStream());
    }
}
